﻿using System;
using Dapper;
using SilenthinkContract.Component;
using SilenthinkContract.Domain;

namespace SilenthinkService
{
    internal static partial class SqlTemplate
    {
        internal static class AuctionTemplate
        {
            #region Auction
            internal static SqlBuilder.Template CreateAuction(Auction o)
            {
                var builder = new SqlBuilder()
                 .Table(DatabaseSchema.AuctionSchema.Table.Auction)
                 .Field("([Name],[ProductId],[ProductVariantId],[Price],[CurrentPrice],[CurrentUserId],[CurrentNickName],[OnlineTime],[BeginTime],[Interval],[ExpectedTime],[ActualTime],[Status],[CreatedBy],[CreatedOn])VALUES(@Name,@ProductId,@ProductVariantId,@Price,@CurrentPrice,@CurrentUserId,@CurrentNickName,@OnlineTime,@BeginTime,@Interval,@ExpectedTime,@ActualTime,@Status,@CreatedBy,@CreatedOn)")
                 .AddParameters(new { o.Name, o.ProductId, o.ProductVariantId, Price = o.PerPrice, o.CurrentPrice, o.CurrentUserId, o.CurrentNickName, o.OnlineTime, BeginTime = o.StartTime, o.Interval, o.ExpectedTime, o.ActualTime, o.Status, o.CreatedBy, o.CreatedOn });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.Insert);
            }

            internal static SqlBuilder.Template UpdateAuction(Auction o)
            {
                var builder = new SqlBuilder()
                    .Table(DatabaseSchema.AuctionSchema.Table.Auction)
                    .Field("[Name]=@Name", new { o.Name })
                    .Field("[ProductId]=@ProductId", new { o.ProductId })
                    .Field("[ProductVariantId]=@ProductVariantId", new { o.ProductVariantId })
                    .Field("[Price]=@Price", new { Price = o.PerPrice })
                    .Field("[CurrentPrice]=@CurrentPrice", new { o.CurrentPrice })
                    .Field("[CurrentUserId]=@CurrentUserId", new { o.CurrentUserId })
                    .Field("[CurrentNickName]=@CurrentNickName", new { o.CurrentNickName })
                    .Field("[OnlineTime]=@OnlineTime", new { o.OnlineTime })
                    .Field("[BeginTime]=@BeginTime", new { BeginTime = o.StartTime })
                    .Field("[Interval]=@Interval", new { o.Interval })
                    .Field("[ExpectedTime]=@ExpectedTime", new { o.ExpectedTime })
                    .Field("[ActualTime]=@ActualTime", new { o.ActualTime })
                    .Field("[Status]=@Status", new { o.Status })
                    .Where("[Id] = @Id", new { o.Id });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.Update);
            }

            internal static SqlBuilder.Template UpdateCurrentAuction(Auction o)
            {
                var builder = new SqlBuilder()
                    .Table(DatabaseSchema.AuctionSchema.Table.Auction)
                    .Field("[CurrentPrice]=@CurrentPrice", new { o.CurrentPrice })
                    .Field("[CurrentUserId]=@CurrentUserId", new { o.CurrentUserId })
                    .Field("[CurrentNickName]=@CurrentNickName", new { o.CurrentNickName })
                    .Field("[ExpectedTime]=@ExpectedTime", new { o.ExpectedTime })
                    .Where("[Id] = @Id", new { o.Id });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.Update);
            }

            internal static SqlBuilder.Template DeleteAuction(long id)
            {
                var builder = new SqlBuilder()
                .Table(DatabaseSchema.AuctionSchema.Table.Auction)
                .Where("[Id] = @Id", new { id });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.Delete);
            }
            internal static SqlBuilder.Template SumBid(long userId, long auctionId, DateTime? begin = null)
            {
                var builder = new SqlBuilder()
                    .Field("ISNULL(SUM([Price]),0) as [Sum]")
                    .Table(DatabaseSchema.AuctionSchema.Table.Bidding)
                    .Where("[UserId] = @UserId", new {userId})
                    .Where("[AuctionId] = @AuctionId", new {auctionId});
                if (begin.HasValue)
                {
                    builder.Where("[CreatedOn]>=@CreatedOn", new {createdOn = begin});
                }
                return builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
            }

            internal static SqlBuilder.Template GetAuction(long id)
            {
                return GetById(id, DatabaseSchema.AuctionSchema.Table.Auction);
            }

            internal static SqlBuilder.Template GetAllAuction()
            {
                var builder = new SqlBuilder()
                .Field("*")
                .Table(DatabaseSchema.AuctionSchema.Table.Auction);

                return builder.AddTemplate(BasicSingleTableSqlTemplate.Select);
            }

            internal static SqlBuilder.Template GetPagedAuction(Condition<Auction> condition = null)
            {
                var builder = new SqlBuilder().Field("*")
                    .Table(DatabaseSchema.AuctionSchema.Table.Auction)
                    .OrderBy("id desc");
                if (condition != null)
                {
                    var content = condition.Conent;
                    if (content != null)
                    {
                        if (content.Id > 0)
                        {
                            builder.Where("[Id]=@Id", new { content.Id });
                        }
                        if (content.ProductId > 0)
                        {
                            builder.Where("[ProductId]=@ProductId", new { content.ProductId });
                        }
                        if (content.CurrentUserId > 0)
                        {
                            builder.Where("[CurrentUserId]=@CurrentUserId", new { content.CurrentUserId });
                        }
                    }
                    if (condition.Size == 1 && condition.Page > 0)
                    {
                        return builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
                    }
                    if (condition.Size > 0 && condition.Page == 0)
                    {
                        return builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopN, new { TakeSize = condition.Size });
                    }
                    if (condition.Size > 0 && condition.Page > 0)
                    {
                        builder.AddParameters(new { begin = condition.BeginRow, end = condition.EndRow });
                        return builder.AddTemplate(BasicSingleTableSqlTemplate.PagedSelect);
                    }
                }

                return builder.AddTemplate(BasicSingleTableSqlTemplate.Select);
            }
            #endregion Auction
            #region BiddingRecord

            internal static SqlBuilder.Template DeleteBidding(int id)
            {
                var builder = new SqlBuilder()
                .Table(DatabaseSchema.AuctionSchema.Table.Bidding)
                .Where("[Id] = @Id", new { id });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.Delete);
            }

            internal static SqlBuilder.Template GetBidding(int id)
            {
                var builder = new SqlBuilder()
                .Field("*")
                .Table(DatabaseSchema.AuctionSchema.Table.Bidding)
                .Where("[Id] = @Id", new { id });
                return builder.AddTemplate(BasicSingleTableSqlTemplate.SelectTopOne);
            }

            internal static SqlBuilder.Template GetAllBidding()
            {
                var builder = new SqlBuilder()
                .Field("*")
                .Table(DatabaseSchema.AuctionSchema.Table.Bidding);

                return builder.AddTemplate(BasicSingleTableSqlTemplate.Select);
            }

            internal static SqlBuilder.Template GetPagedBidding(int index = 1, int size = 20)
            {
                if (index < 1)
                {
                    index = 1;
                }
                if (size < 0 || size > 200)
                {
                    size = 20;
                }
                var builder = new SqlBuilder().Field("*")
                .Table(DatabaseSchema.AuctionSchema.Table.Bidding)
                .OrderBy("id desc")
                .AddParameters(new { begin = (index - 1) * size + 1, end = index * size });
                /*
                .Where("");
                */
                return builder.AddTemplate(BasicSingleTableSqlTemplate.PagedSelect);
            }
            #endregion BiddingRecord
        }
    }
}